package com.geek.javase.ch16.work.work1;

/*
 *@author wangpeng
 *2025/11/2
 *@description TODO
 */
import java.util.concurrent.PriorityBlockingQueue;

public class PriorityBlockingQueueTest{
    public static void main(String[] args) {
        // 创建优先级阻塞队列，用于线程间通信
        // 我们将使用整数作为信号，通过优先级控制执行顺序
        PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();

        // 线程1 - 打印A和C
        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("A");

                // 等待接收优先级为2的信号（来自线程2）
                int signal = queue.take();
                System.out.println("线程1收到信号: " + signal);

                System.out.println("C");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        // 线程2 - 打印B
        Thread thread2 = new Thread(() -> {
            try {
                Thread.sleep(100); // 确保线程1先打印A
                System.out.println("B");

                // 发送优先级为2的信号给线程1
                queue.put(2);
                System.out.println("线程2发送信号: 2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start();
        thread2.start();
    }
}